<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- ss | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- ss</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ss是Socket Statistics的缩写。顾名思义，ss命令可以用来获取socket统计信息，它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息，而且比netstat更快速更高效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当服务器的socket连接数量变得非常大时，无论是使用netstat命令还是直接cat /proc/net/tcp，执行速度都会很慢。可能你不会有切身的感受，但请相信我，当服务器维持的连接达到上万个的时候，使用netstat等于浪费 生命，而用ss才是节省时间。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;天下武功唯快不破。ss快的秘诀在于，它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块，可以获得Linux 内核中第一手的信息，这就确保了ss的快捷高效。当然，如果你的系统中没有tcp_diag，ss也可以正常运行，只是效率会变得稍慢。（但仍然比 netstat要快。）</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ss [参数]</div><div class="line">ss [参数] [过滤]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ss(Socket Statistics的缩写)命令可以用来获取 socket统计信息，此命令输出的结果类似于 netstat输出的内容，但它能显示更多更详细的 TCP连接状态的信息，且比 netstat 更快速高效。它使用了 TCP协议栈中 tcp_diag（是一个用于分析统计的模块），能直接从获得第一手内核信息，这就使得 ss命令快捷高效。在没有 tcp_diag，ss也可以正常运行。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3.命令参数"></a>3.命令参数</h2><ul>
<li>-h, –help    帮助信息</li>
<li>-V, –version    程序版本信息</li>
<li>-n, –numeric    不解析服务名称</li>
<li>-r, –resolve        解析主机名</li>
<li>-a, –all    显示所有套接字（sockets）</li>
<li>-l, –listening    显示监听状态的套接字（sockets）</li>
<li>-o, –options        显示计时器信息</li>
<li>-e, –extended       显示详细的套接字（sockets）信息</li>
<li>-m, –memory         显示套接字（socket）的内存使用情况</li>
<li>-p, –processes    显示使用套接字（socket）的进程</li>
<li>-i, –info    显示 TCP内部信息</li>
<li>-s, –summary    显示套接字（socket）使用概况</li>
<li>-4, –ipv4           仅显示IPv4的套接字（sockets）</li>
<li>-6, –ipv6           仅显示IPv6的套接字（sockets）</li>
<li>-0, –packet            显示 PACKET 套接字（socket）</li>
<li>-t, –tcp    仅显示 TCP套接字（sockets）</li>
<li>-u, –udp    仅显示 UCP套接字（sockets）</li>
<li>-d, –dccp    仅显示 DCCP套接字（sockets）</li>
<li>-w, –raw    仅显示 RAW套接字（sockets）</li>
<li>-x, –unix    仅显示 Unix套接字（sockets）</li>
<li>-f, –family=FAMILY  显示 FAMILY类型的套接字（sockets），FAMILY可选，支持  unix, inet, inet6, link, netlink</li>
<li>-A, –query=QUERY, –socket=QUERY<ul>
<li>QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]</li>
</ul>
</li>
<li>-D, –diag=FILE     将原始TCP套接字（sockets）信息转储到文件</li>
<li>-F, –filter=FILE   从文件中都去过滤器信息<ul>
<li>FILTER := [ state TCP-STATE ] [ EXPRESSION ]</li>
</ul>
</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4.使用实例"></a>4.使用实例</h2><h3 id="实例1：显示TCP连接"><a href="#实例1：显示TCP连接" class="headerlink" title="实例1：显示TCP连接"></a>实例1：显示TCP连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -t -a</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -t -a</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">LISTEN     0      0                                         127.0.0.1:smux                                               *:*       </div><div class="line">LISTEN     0      0                                                 *:3690                                               *:*       </div><div class="line">LISTEN     0      0                                                 *:ssh                                                *:*       </div><div class="line">ESTAB      0      0                                   192.168.120.204:ssh                                        10.2.0.68:49368   </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例2：显示-Sockets-摘要"><a href="#实例2：显示-Sockets-摘要" class="headerlink" title="实例2：显示 Sockets 摘要"></a>实例2：显示 Sockets 摘要</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -s</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -s</span></div><div class="line">Total: 34 (kernel 48)</div><div class="line">TCP:   4 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 3</div><div class="line"></div><div class="line">Transport Total     IP        IPv6</div><div class="line">*         48        -         -        </div><div class="line">RAW       0         0         0        </div><div class="line">UDP       5         5         0        </div><div class="line">TCP       4         4         0        </div><div class="line">INET      9         9         0        </div><div class="line">FRAG      0         0         0        </div><div class="line"></div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列出当前的established, closed, orphaned and waiting TCP sockets</p>
<h3 id="实例3：列出所有打开的网络连接端口"><a href="#实例3：列出所有打开的网络连接端口" class="headerlink" title="实例3：列出所有打开的网络连接端口"></a>实例3：列出所有打开的网络连接端口</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -l</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -l</span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">0      0                                              127.0.0.1:smux                                                    *:*       </div><div class="line">0      0                                                      *:3690                                                    *:*       </div><div class="line">0      0                                                      *:ssh                                                     *:*       </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例4：查看进程使用的socket"><a href="#实例4：查看进程使用的socket" class="headerlink" title="实例4：查看进程使用的socket"></a>实例4：查看进程使用的socket</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -pl</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -pl</span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">0      0                                              127.0.0.1:smux                                                    *:*        users:((<span class="string">"snmpd"</span>,2716,8))</div><div class="line">0      0                                                      *:3690                                                    *:*        users:((<span class="string">"svnserve"</span>,3590,3))</div><div class="line">0      0                                                      *:ssh                                                     *:*        users:((<span class="string">"sshd"</span>,2735,3))</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例5：找出打开套接字-端口应用程序"><a href="#实例5：找出打开套接字-端口应用程序" class="headerlink" title="实例5：找出打开套接字/端口应用程序"></a>实例5：找出打开套接字/端口应用程序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -lp | grep 3306</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -lp|grep 1935</span></div><div class="line">0      0                            *:1935                          *:*        users:((<span class="string">"fmsedge"</span>,2913,18))</div><div class="line">0      0                    127.0.0.1:19350                         *:*        users:((<span class="string">"fmsedge"</span>,2913,17))</div><div class="line">[root@localhost ~]<span class="comment"># ss -lp|grep 3306</span></div><div class="line">0      0                            *:3306                          *:*        users:((<span class="string">"mysqld"</span>,2871,10))</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例6：显示所有UDP-Sockets"><a href="#实例6：显示所有UDP-Sockets" class="headerlink" title="实例6：显示所有UDP Sockets"></a>实例6：显示所有UDP Sockets</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -u -a</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -u -a</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">UNCONN     0      0                                         127.0.0.1:syslog                                             *:*       </div><div class="line">UNCONN     0      0                                                 *:snmp                                               *:*       </div><div class="line">ESTAB      0      0                                   192.168.120.203:39641                                  10.58.119.119:domain </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例7：显示所有状态为established的SMTP连接"><a href="#实例7：显示所有状态为established的SMTP连接" class="headerlink" title="实例7：显示所有状态为established的SMTP连接"></a>实例7：显示所有状态为established的SMTP连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -o state established <span class="string">'( dport = :smtp or sport = :smtp )'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -o state established '( dport = :smtp or sport = :smtp )' </span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例8：显示所有状态为Established的HTTP连接"><a href="#实例8：显示所有状态为Established的HTTP连接" class="headerlink" title="实例8：显示所有状态为Established的HTTP连接"></a>实例8：显示所有状态为Established的HTTP连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -o state established <span class="string">'( dport = :http or sport = :http )'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -o state established '( dport = :http or sport = :http )' </span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">0      0                                              75.126.153.214:2164                                        192.168.10.42:http    </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例9：列举出处于-FIN-WAIT-1状态的源端口为-80或者-443，目标网络为-193-233-7-24所有-tcp套接字"><a href="#实例9：列举出处于-FIN-WAIT-1状态的源端口为-80或者-443，目标网络为-193-233-7-24所有-tcp套接字" class="headerlink" title="实例9：列举出处于 FIN-WAIT-1状态的源端口为 80或者 443，目标网络为 193.233.7/24所有 tcp套接字"></a>实例9：列举出处于 FIN-WAIT-1状态的源端口为 80或者 443，目标网络为 193.233.7/24所有 tcp套接字</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -o state fin-wait-1 <span class="string">'( sport = :http or sport = :https )'</span> dst 193.233.7/24</div></pre></td></tr></table></figure>
<h3 id="实例10：用TCP-状态过滤Sockets"><a href="#实例10：用TCP-状态过滤Sockets" class="headerlink" title="实例10：用TCP 状态过滤Sockets:"></a>实例10：用TCP 状态过滤Sockets:</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ss -4 state FILTER-NAME-HERE </div><div class="line">ss -6 state FILTER-NAME-HERE</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment">#ss -4 state closing </span></div><div class="line">Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port </div><div class="line">1      11094                                                  75.126.153.214:http                                                      192.168.10.42:4669</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FILTER-NAME-HERE 可以代表以下任何一个：</p>
<ul>
<li>established      </li>
<li>syn-sent      </li>
<li>syn-recv      </li>
<li>fin-wait-1      </li>
<li>fin-wait-2      </li>
<li>time-wait      </li>
<li>closed      </li>
<li>close-wait      </li>
<li>last-ack      </li>
<li>listen      </li>
<li>closing      </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;all : 所有以上状态</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;connected : 除了listen and closed的所有状态</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;synchronized :所有已连接的状态除了syn-sent</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bucket : 显示状态为maintained as minisockets,如：time-wait和syn-recv.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;big : 和bucket相反.</p>
<h3 id="实例11：匹配远程地址和端口号"><a href="#实例11：匹配远程地址和端口号" class="headerlink" title="实例11：匹配远程地址和端口号"></a>实例11：匹配远程地址和端口号</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">ss dst ADDRESS_PATTERN</div><div class="line">ss dst 192.168.1.5</div><div class="line">ss dst 192.168.119.113:http </div><div class="line">ss dst 192.168.119.113:smtp </div><div class="line">ss dst 192.168.119.113:443</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss dst 192.168.119.113</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:20229   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:61056   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:61623   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:60924   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16050                                192.168.119.113:43701   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16073                                192.168.119.113:32930   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16073                                192.168.119.113:49318   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:3844    </div><div class="line">[root@localhost ~]<span class="comment"># ss dst 192.168.119.113:http</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">[root@localhost ~]<span class="comment"># ss dst 192.168.119.113:3844</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:3844    </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例12：匹配本地地址和端口号"><a href="#实例12：匹配本地地址和端口号" class="headerlink" title="实例12：匹配本地地址和端口号"></a>实例12：匹配本地地址和端口号</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">ss src ADDRESS_PATTERN</div><div class="line">ss src 192.168.119.103</div><div class="line">ss src 192.168.119.103:http</div><div class="line">ss src 192.168.119.103:80</div><div class="line">ss src 192.168.119.103:smtp</div><div class="line">ss src 192.168.119.103:25</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss src 192.168.119.103:16021</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:63054   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:62894   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:63055   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:2274    </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:44784   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:7233    </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.103:58660   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:44822   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56737   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:57487   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56736   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:64652   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56586   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:64653   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56587   </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例13：将本地或者远程端口和一个数比较"><a href="#实例13：将本地或者远程端口和一个数比较" class="headerlink" title="实例13：将本地或者远程端口和一个数比较"></a>实例13：将本地或者远程端口和一个数比较</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ss dport OP PORT </div><div class="line">ss sport OP PORT</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss  sport = :http </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  dport = :http </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  dport \&gt; :1024 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  sport \&gt; :1024 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss sport \&lt; :32000 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  sport eq :22 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  dport != :22 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  state connected sport = :http </span></div><div class="line">[root@localhost ~]<span class="comment"># ss \( sport = :http or sport = :https \) </span></div><div class="line">[root@localhost ~]<span class="comment"># ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ss dport OP PORT 远程端口和一个数比较；ss sport OP PORT 本地端口和一个数比较。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>OP 可以代表以下任意一个:</strong> </p>
<ul>
<li>&lt;= or le : 小于或等于端口号</li>
<li><blockquote>
<p>= or ge : 大于或等于端口号</p>
</blockquote>
</li>
<li>== or eq : 等于端口号</li>
<li>!= or ne : 不等于端口号</li>
<li>&lt; or lt : 小于端口号</li>
<li><blockquote>
<p>or gt : 大于端口号</p>
</blockquote>
</li>
</ul>
<h3 id="实例14：ss-和-netstat-效率对比"><a href="#实例14：ss-和-netstat-效率对比" class="headerlink" title="实例14：ss 和 netstat 效率对比"></a>实例14：ss 和 netstat 效率对比</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">time netstat -at</div><div class="line">time ss</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># time ss   </span></div><div class="line">real    0m0.739s</div><div class="line">user    0m0.019s</div><div class="line">sys     0m0.013s</div><div class="line">[root@localhost ~]<span class="comment"># </span></div><div class="line">[root@localhost ~]<span class="comment"># time netstat -at</span></div><div class="line">real    2m45.907s</div><div class="line">user    0m0.063s</div><div class="line">sys     0m0.067s</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用time 命令分别获取通过netstat和ss命令获取程序和概要占用资源所使用的时间。在服务器连接数比较多的时候，netstat的效率完全没法和ss比。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/59. Linux 命令- ss/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
